import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: 排列距离
 * User: DELL
 * Date: 2024-05-24
 * Time: 9:02
 */
public class Main2 {
    static int N = 17;
    static long ALL = 355687428096000L;
    static boolean vis[] = new boolean[N];
    static char[] ch = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'n', 'o', 'p', 'q', 'r'};

    public static void main(String[] args) {
        String str1 = "aejcldbhpiogfqnkr";
        String str2 = "ncfjboqiealhkrpgd";
        long temp1 = getPre(str1, 0);
        Arrays.fill(vis, false);
        long temp2 = getPre(str2, 0);
        Arrays.fill(vis, false);
        long num1 = temp2 - temp1;
        long num2 = temp1 + ALL - temp2;
        System.out.println(Math.min(num1, num2));
        //106148357572143
    }

    private static long fun(int n) {
        long res = 1;
        for (int i = 1; i <= n; i++) {
            res *= i;
        }
        return res;
    }

    private static long getPre(String str, int i) {
        if (i == str.length()) {
            return 1;
        }
        long res = 0;
        // 统计
        long temp = 0;
        for (int j = 0; j < N; j++) {
            if (!vis[j]) {
                if (ch[j] < str.charAt(i)) {
                    temp++;
                }
            }
        }
        res += temp * fun(str.length() - i - 1);
        if (str.charAt(i) >= 'n') {
            vis[str.charAt(i) - 'a' - 1] = true;
        } else {
            vis[str.charAt(i) - 'a'] = true;
        }
        res += getPre(str, i + 1);
        return res;
    }
}
